package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.ICrmCustomerImportDetailsDAO;
import org.jsola.hr.dto.CrmCustomerImportDetailsAddDTO;
import org.jsola.hr.dto.CrmCustomerImportDetailsUpdateDTO;
import org.jsola.hr.entity.CrmCustomerImportDetailsDO;
import org.jsola.hr.query.CrmCustomerImportDetailsQuery;
import org.jsola.hr.service.ICrmCustomerImportDetailsService;
import org.jsola.hr.vo.CrmCustomerImportDetailsListVO;
import org.jsola.hr.vo.CrmCustomerImportDetailsVO;
import org.jsola.hr.vo.CrmCustomerImportErrorDataVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 导入客户记录详情表
 *
 * @author zhr
 */
@Service("hrCrmCustomerImportDetailsServiceImpl")
public class CrmCustomerImportDetailsServiceImpl implements ICrmCustomerImportDetailsService {

    @Autowired
    private ICrmCustomerImportDetailsDAO crmCustomerImportDetailsDAO;

    @Override
    public Page<CrmCustomerImportErrorDataVO> getErrorData(CrmCustomerImportDetailsQuery crmCustomerImportDetailsQuery, TokenUser tokenUser) {
        Page<CrmCustomerImportDetailsListVO> page = selectPage(crmCustomerImportDetailsQuery, tokenUser.getSiteId());
        List<CrmCustomerImportErrorDataVO> crmCustomerImportErrorDataVOS = new ArrayList<>();
        for (CrmCustomerImportDetailsListVO entity : page.getEntities()) {
            CrmCustomerImportErrorDataVO crmCustomerImportErrorDataVO = new CrmCustomerImportErrorDataVO();
            crmCustomerImportErrorDataVO.setCustomerImportDetailsListVO(entity);
            crmCustomerImportErrorDataVO.setErrorMsg(entity.getErrorReason());
            crmCustomerImportErrorDataVOS.add(crmCustomerImportErrorDataVO);
        }
        Page<CrmCustomerImportErrorDataVO> crmCustomerImportErrorDataVOPage = new Page<>();
        crmCustomerImportErrorDataVOPage.setEntities(crmCustomerImportErrorDataVOS);
        crmCustomerImportErrorDataVOPage.setEntityCount(page.getEntityCount());
        crmCustomerImportErrorDataVOPage.setFirstEntityIndex(page.getFirstEntityIndex());
        crmCustomerImportErrorDataVOPage.setLastEntityIndex(page.getLastEntityIndex());
        crmCustomerImportErrorDataVOPage.setPageCount(page.getPageCount());
        crmCustomerImportErrorDataVOPage.setPageNo(page.getPageNo());
        crmCustomerImportErrorDataVOPage.setPageSize(page.getPageSize());
        return crmCustomerImportErrorDataVOPage;
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public CrmCustomerImportDetailsVO save(CrmCustomerImportDetailsAddDTO crmCustomerImportDetailsAddDTO, TokenUser tokenUser) {
        // 转DO
        CrmCustomerImportDetailsDO crmCustomerImportDetailsDO = crmCustomerImportDetailsAddDTO.to(CrmCustomerImportDetailsDO.class);
        // 保存
        crmCustomerImportDetailsDO = save(crmCustomerImportDetailsDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return crmCustomerImportDetailsDO.to(CrmCustomerImportDetailsVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(CrmCustomerImportDetailsUpdateDTO crmCustomerImportDetailsUpdateDTO, TokenUser tokenUser) {
        //转DO
        CrmCustomerImportDetailsDO crmCustomerImportDetailsDO = crmCustomerImportDetailsUpdateDTO.to(CrmCustomerImportDetailsDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(crmCustomerImportDetailsDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... crmCustomerImportDetailsIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) crmCustomerImportDetailsIds);
    }


    @Override
    public CrmCustomerImportDetailsVO selectById(String crmCustomerImportDetailsId, String siteId) {
        CrmCustomerImportDetailsDO crmCustomerImportDetailsDO = selectDOById(crmCustomerImportDetailsId, siteId);
        if (crmCustomerImportDetailsDO == null) {
            return null;
        }
        return crmCustomerImportDetailsDO.to(CrmCustomerImportDetailsVO.class);
    }

    @Override
    public List<CrmCustomerImportDetailsListVO> select(CrmCustomerImportDetailsQuery crmCustomerImportDetailsQuery, String siteId) {
        List<CrmCustomerImportDetailsDO> crmCustomerImportDetailsDOList = selectDO(crmCustomerImportDetailsQuery, siteId);
        if(CollectionUtils.isEmpty(crmCustomerImportDetailsDOList)) {
            return crmCustomerImportDetailsDOList == null ? null : new ArrayList<>();
        }
        return crmCustomerImportDetailsDOList.stream()
                .map(crmCustomerImportDetailsDO -> crmCustomerImportDetailsDO.to(CrmCustomerImportDetailsListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(CrmCustomerImportDetailsQuery crmCustomerImportDetailsQuery, String siteId) {
        Example example = buildExample(crmCustomerImportDetailsQuery,siteId);
        return crmCustomerImportDetailsDAO.selectCountByExample(example);
    }

    @Override
    public Page<CrmCustomerImportDetailsListVO> selectPage(CrmCustomerImportDetailsQuery crmCustomerImportDetailsQuery, String siteId) {
        Example example = buildExample(crmCustomerImportDetailsQuery,siteId);
        Page<CrmCustomerImportDetailsDO> page = crmCustomerImportDetailsDAO.selectPageByExample(example,
                crmCustomerImportDetailsQuery.getPageNo(),
                crmCustomerImportDetailsQuery.getPageSize());

        return page.to(CrmCustomerImportDetailsListVO.class);
    }

    @Override
    public CrmCustomerImportDetailsDO selectDOById(String crmCustomerImportDetailsId, String siteId) {
        return listById(crmCustomerImportDetailsId, siteId);
    }

    @Override
    public List<CrmCustomerImportDetailsDO> selectDO(CrmCustomerImportDetailsQuery crmCustomerImportDetailsQuery, String siteId) {
        Example example = buildExample(crmCustomerImportDetailsQuery,siteId);
        return crmCustomerImportDetailsDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param crmCustomerImportDetailsQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(CrmCustomerImportDetailsQuery crmCustomerImportDetailsQuery, String siteId) {
        Example example = new Example(CrmCustomerImportDetailsDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        if(!StringUtils.isEmpty(crmCustomerImportDetailsQuery.getPhone())){
            example.and().andEqualTo("phone",crmCustomerImportDetailsQuery.getPhone());
        }
        if(crmCustomerImportDetailsQuery.getIsError() != null){
            example.and().andEqualTo("isError",crmCustomerImportDetailsQuery.getIsError());
        }
        if(!StringUtils.isEmpty(crmCustomerImportDetailsQuery.getRecordId())){
            example.and().andEqualTo("recordId",crmCustomerImportDetailsQuery.getRecordId());
        }
        if(!StringUtils.isEmpty(crmCustomerImportDetailsQuery.getCompanyName())){
            example.and().andEqualTo("companyName",crmCustomerImportDetailsQuery.getCompanyName());
        }
        ExampleKit.setExampleOrder(example,crmCustomerImportDetailsQuery.getOrders());
        return example;
    }
}




